{% extends 'layout.html' %}
{% load static %}

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">折线图</div>
        <div class="panel-body">
            <div id="line" style="width: 600px;height: 400px"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-8">
            <div class="panel panel-default">
                <div class="panel-heading">柱状图</div>
                <div class="panel-body">
                    <div id="bar" style="height: 400px"></div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}




{% block js %}
    <script src="{% static 'js/echarts.min.js' %}"></script>
    <script type="text/javascript">

        $(function () {
            initLine()
            initBar()
        })

        /*
        * 初始化
        * */

        function initBar() {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('bar'));
            option = {
                xAxis: {
                    data: []    // 后台获取
                },
                legend: {
                    data:['111','222']
                },
                yAxis: {
                    type: 'value'
                },
                tooltip: {
                  axisPointer:{
                      type:'shadow'
                  }
                },
                series: []   // 后台获取
            };
            myChart.setOption(option);
            $.ajax({
                url:"/chart/bar/",
                type:"get",
                dataType: "JSON",
                success:function(res){
                    if(res.status){
                        console.log(res)
                        option.xAxis.data = res.data.xAxis
                        option.series = res.data.series_list
                        myChart.setOption(option);
                    }
                }
            })
        }


        function initLine() {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('line'));

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: 'ECharts 入门示例',
                    left: "center"
                },
                tooltip: {
                    axisPointer:{
                      type:'shadow'
                  }
                },
                legend: {
                    data: [],   // 后台获取
                    bottom: 0
                },
                xAxis: {
                    data: []    // 后台获取
                },
                yAxis: {},
                series: []   // 后台获取
            };
            $.ajax({
                url: "/chart/line/",
                type: "get",
                dataType: "JSON",
                success: function (res) {
                    // 将后台返回的数据更新到option中
                    if (res.status) {
                        option.legend.data = res.data.legend;
                        option.xAxis.data = res.data.xAxis;
                        option.series = res.data.series_list;
                        myChart.setOption(option);
                    }

                }
            })
        }

    </script>
{% endblock %}